﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsGame4
{
    class StateMachine
    {
        private AIObject aiObject;
        private State currentState;
        private State previousState;

        public StateMachine(AIObject aiObject)
        {
            this.aiObject = aiObject;
            currentState = null;
            previousState = null;
        }

        public void update()
        {
            if(currentState != null)
                currentState.execute(aiObject);
        }

        public void changeState(State newState)
        {
            // Save previous state
            previousState = currentState;
            // Exit current state
            currentState.exit(aiObject);
            // Change current state
            currentState = newState;
            // Enter current state
            currentState.enter(aiObject);
        }

        public void returnPreviousState()
        {
            changeState(previousState);
        }

        public Boolean equalState(State state)
        {
            return state.Name == currentState.Name;
        }

        public State CurrentState
        {
            get { return currentState; }
            set { currentState = value; }
        }

        public State PreviousState
        {
            get { return previousState; }
            set { previousState = value; }
        }

        public Boolean HandleMessage(Message message)
        {
            if(currentState != null && currentState.onMessage(aiObject,message) == true)
                return true;
            return false;
        }
    }
}